<?php
	class Utilities {
		public static function RealPrefix() {
			return str_replace('\\', '/', dirname(__FILE__));
		}
		
		public static function SystemCheck() {
			try {
				DB::Get();
			}
			catch(DBConnectionException $ex) {
				return 1;
			}
			
			if(!file_exists(self::RealPrefix() . '/../templates/' . Settings::Get('template'))) {
				return 2;
			}
			
			return 0;
		}
		
		public static function DeterminePlural($word, $number) {
			if($number != 1) {
				return self::Plural($word);
			}
			
			return $word;
		}
		
		public static function Plural($word) {
			switch($word) {
				case 'category':
					return 'categories';
				default:
					return ($word . 's');
			}
		}
		
		public static function GetAction($default = 'view') {
			return ((isset($_REQUEST['action'])) ? $_REQUEST['action'] : $default);
		}
		
		public static function URLSafe($title) {
			return trim(strtolower(preg_replace("/[^a-zA-Z0-9]/", "-", $title)), '-');
		}
		
		public static function CreatePrettyLink($content_type, $content_id) {
			$link = Settings::Get('blog_url') . '/';
			
			switch($content_type) {
				case 'post':
					$post = Post::Get($content_id);
					$link .= date('Y/m/d', $post['time']);
					$link .= '/' . self::URLSafe($post['title']);
					break;
				case 'page':
					$page = Page::Get($content_id);
					$link .= '/' . self::URLSafe($page['title']);
					break;
				case 'category':
					$category = Category::Get($content_id);
					$link .= 'categories/' . self::URLSafe($category['title']);
					break;
				case 'user':
					$user = User::GetInfoById($content_id);
					$link .= 'users/' . self::URLSafe($user['username']);
					break;
				case 'file':
					//$file = File::Get($content_id);
					//$link .= 'files/' . self::URLSafe($file['title']);
					break;
			}
			
			return $link;
		}
	}
?>